package cn.tedu.api;

public class TestString {
    public static void main(String[] args) {
        String s="abcdefghijklmnopqrstuvwxyz";
       //method(s);
        method2(s);
    }
    /*优化1：用StringBuffer和StringBuilder*/
    private static void method2(String s) {
        StringBuffer s1=new StringBuffer();
        StringBuilder s2=new StringBuilder();
        long t1=System.currentTimeMillis();
        for (int i=1;i<10001;i++){
            /*优化2：不用+拼接，使用append（）*/
            //s1.append(s);
            s2.append(s);
            //System.out.println(s1.append(s));
        }
        long t2=System.currentTimeMillis();
        //System.out.println(s);
        //System.out.println(s1.append(s));
        //System.out.println(s1);
        System.out.println(s2);
        //System.out.println(s1.toString());
        System.out.println(s2.toString());
        System.out.println(t2-t1);
    }

    private static void method(String s) {
        String r="";//不能写null，否则出现的结果会拼接null，长度也会变成260004
        long t1 =System.currentTimeMillis();
        System.out.println(t1);
        for (int i=1;i<10001;i++){
             r=s+r;
        }
        long t2 =System.currentTimeMillis();
        System.out.println(t2);
        System.out.println(t2-t1);
        System.out.println(r);
        System.out.println(r.length());




    }
}
